package me.jessyan.mvparms.demo.mvp.ui.fragment;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.github.megatronking.svg.support.SVGDrawable;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.base.BaseHolder;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DoubleClickUtils;
import com.jess.arms.utils.PerfectClickListener;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.di.component.fragment.DaggerLifeComponent;
import me.jessyan.mvparms.demo.di.module.fragment.LifeModule;
import me.jessyan.mvparms.demo.drawables.ic_arrow_right;
import me.jessyan.mvparms.demo.drawables.ic_yellow_exclamation_point;
import me.jessyan.mvparms.demo.mvp.contract.fragment.LifeContract;
import me.jessyan.mvparms.demo.mvp.presenter.fragment.LifePresenter;
import me.jessyan.mvparms.demo.mvp.ui.activity.car.ChoiceCarActivity;
import me.jessyan.mvparms.demo.mvp.ui.activity.coupon.GetCouponActivity;
import me.jessyan.mvparms.demo.mvp.ui.activity.integral.IntegralActivity;
import me.jessyan.mvparms.demo.mvp.ui.activity.shoppcard.InvalidCardActivity;
import me.jessyan.mvparms.demo.mvp.ui.activity.specialoffer.SpecialOfferActivity;
import me.jessyan.mvparms.demo.mvp.ui.activity.store.StoreActivity;
import me.jessyan.mvparms.demo.mvp.ui.widget.RoundImageView;
import me.jessyan.mvparms.demo.mvp.ui.widget.banner.BannerViewPager;
import me.jessyan.mvparms.demo.mvp.ui.widget.banner.PageBean;
import me.jessyan.mvparms.demo.mvp.ui.widget.banner.PageHelpterListener;
import me.jessyan.mvparms.demo.mvp.ui.widget.banner.ZoomIndicator;
import me.jessyan.mvparms.demo.mvp.ui.widget.tsnackbar.TSnackbar;
import me.jessyan.mvparms.demo.utils.NetSnackbarUtil;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * 主页默认一级 fragment
 */
public class LifeFragment extends OneLevelBaseFragment<LifePresenter> implements LifeContract.View, View.OnClickListener {

    BannerViewPager bannerOne;
    BannerViewPager bannerTwo;
    ZoomIndicator indicator;
    FrameLayout contentFl;
    RecyclerView recyclerView;
    @Inject
    RxPermissions mRxPermissions;
    private ImageView scoreIv;


    public static LifeFragment newInstance() {
        LifeFragment fragment = new LifeFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerLifeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .lifeModule(new LifeModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.homr_life_framgent, container, false);
        bannerOne = view.findViewById(R.id.banner_one);
        bannerTwo = view.findViewById(R.id.banner_two);
        indicator = view.findViewById(R.id.indicator);
        scoreIv = view.findViewById(R.id.score_iv);
        contentFl = view.findViewById(R.id.content_fl);
        recyclerView = view.findViewById(R.id.recyclerView);
        scoreIv.setOnClickListener(this);
        view.findViewById(R.id.stopCar_iv).setOnClickListener(this);
        view.findViewById(R.id.active_iv).setOnClickListener(this);
        view.findViewById(R.id.scan_iv).setOnClickListener(this);
        view.findViewById(R.id.ticket_iv).setOnClickListener(this);

        return view;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        initBanner();
        showFragmentError();
        initRecycler();
    }

    private void initBanner() {
        // todo 自动播放的开启和停止
        List<Integer> icons = new ArrayList<>();
        icons.add(R.mipmap.beauty1);
        icons.add(R.mipmap.beauty2);
        icons.add(R.mipmap.beauty3);
        icons.add(R.mipmap.beauty4);

        PageBean bean = new PageBean.Builder<Integer>()
                .setDataObjects(icons)
                .setIndicator(null)
                .builder();

        bannerOne.setPageMargin(ArmsUtils.dip2px(getContext(), 8));
        bannerOne.setPageListener(bean, R.layout.item_life_banner_one, new PageHelpterListener<Integer>() {
            @Override
            public void getItemView(View view, Integer data) {
                ImageView imageView = (ImageView) view;
                imageView.setBackgroundResource(data);
            }
        });
        indicator.addPagerData(null, icons.size(), bannerOne);
        bannerOne.startAnim();

        //第二年个banner


        bannerTwo.setmTransformer(200, 0.2f);
        bannerTwo.setPageListener(bean, R.layout.item_life_banner_two, new PageHelpterListener<Integer>() {
            @Override
            public void getItemView(View view, Integer data) {
                RoundImageView imageView = view.findViewById(R.id.loop_icon);

                imageView.setImageResource(data);
            }
        });
    }

    private void initRecycler() {
        ArrayList<String> list = new ArrayList<>();

        list.add("s");
        list.add("s");
        list.add("s");
        list.add("s");
        list.add("s");
        list.add("s");
        list.add("s");
        list.add("s");
        GridLayoutManager gl = new GridLayoutManager(getContext(), 4);
        ArmsUtils.configRecyclerView(recyclerView, gl);
        recyclerView.setLayoutManager(gl);
        DefaultAdapter<String> adapter = new DefaultAdapter<String>(list, false) {
            @Override
            public BaseHolder<String> getHolder(View v, int viewType) {
                return new BaseHolder<String>(v) {
                    @Override
                    public void setData(String data, int position) {
                        ImageView imageView = v.findViewById(R.id.icon_iv);
                        imageView.setOnClickListener(new PerfectClickListener() {
                            @Override
                            protected void onNoDoubleClick(View v) {
                                if (mOnItemClickListener != null) {
                                    mOnItemClickListener.onItemClick(v, viewType, data, position);
                                }
                            }
                        });
                    }
                };
            }

            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_life_frag_hot_brand;
            }
        };
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new DefaultAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int viewType, Object data, int position) {
                if (view instanceof ImageView) {
                    Intent intent = new Intent(getActvit(), StoreActivity.class);
                    ActivityOptionsCompat options =
                            ActivityOptionsCompat.makeSceneTransitionAnimation(getActvit(),
                                    view, getResources().getString(R.string.transition_store_img));//与xml文件对应
                    ActivityCompat.startActivity(getActvit(), intent, options.toBundle());
                }
            }
        });
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void onResume() {
        super.onResume();
        NetSnackbarUtil.getInstance().checkAndShow(contentFl, getActivity());
    }

    @Override
    public void onReceiveBack() {

        if (isVisible) NetSnackbarUtil.getInstance().checkAndShow(contentFl, getActivity());
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) NetSnackbarUtil.getInstance().checkAndShow(contentFl, getActivity());
    }

    @Override
    protected void onRefresh() {
        super.onRefresh();
        showMessage("刷新");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                showFragementContentView();

            }
        }, 3000);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }


    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @Override
    public void onClick(View view) {

        if (DoubleClickUtils.getInstance().isDouble(view)) return;
        switch (view.getId()) {
            //领券中心
            case R.id.ticket_iv:
                startActivity(new Intent(getActivity(), GetCouponActivity.class));
                break;
            //积分
            case R.id.score_iv:
                startActivity(new Intent(getActivity(), IntegralActivity.class));
                break;
            case R.id.active_iv:
                startActivity(new Intent(getActivity(), SpecialOfferActivity.class));
                break;
            //停车缴费
            case R.id.stopCar_iv:
                startActivity(new Intent(getActivity(), ChoiceCarActivity.class));
                break;
            case R.id.scan_iv:
                mPresenter.scanCode();
                break;
            default:
        }
    }

    @Override
    public RxPermissions getRxPermissions() {
        return mRxPermissions;
    }

    @Override
    public Fragment getFragment() {
        return this;
    }

    @Override
    public Activity getActvit() {
        return getActivity();
    }
}
